import { getCommodityDetail } from '../../../../services/commodity';
import * as log from '../../../../utils/log';

const app = getApp();

const URL = app.globalData.URL;

let orderList = [];

Component({
  mixins: [],
  data: {
    tabs: ['商品介绍', '租赁流程','租赁必读'],
    src:"../../../image/sp/spjs.png",
    src_status:"true",
    src1:URL+'/admin-api/infra/file/4/get/b3a10951f9307d6b22e4f3880e2ff2fcd267d4fe68456077ded7a7d76f5e01ae.png',
    activeTab: 0, 
    titleOpacity: 1,
    shadow: false,
    orderList: [],
    selectedShippingMethodId: '',
    selectedOptionIds: [],
    currentCommodity: {},
    purchaseNumber: 1,
    address: '河南省郑州市金水区文化路xx号 橙黄蓝车行',
  },

  props: {
    show: false,
    onClose: () => {},
    onConfirm: () => {},
    commodityId: '',
  },
  
  didMount() {},
  didUpdate(preProps) {
    const { commodityId } = this.props;
    if (preProps.commodityId !== commodityId) {
      this.fetchCommodityDetailById(commodityId);
      this.getGoodsById(commodityId);
    }
  },
  didUnmount() {},
  methods: {
    onClose() {
      this.props.onClose();
    },

    getGoodsById(id){

      my.httpRequest({
        url: `${URL}/app-api/chl/goods/get`,
        data: {
          id:id
        },
        headers:{"tenant-id":app.globalData.tenant_id},
        success: (res) => {
          console.log("获取选中商品---");
          console.log(res.data.data);
          this.setData({
            currentCommodity:res.data.data,
          })

        },
        fail: (err) => {
          console.log("失败",err);
        }
      });
    },
    fetchCommodityDetailById(id) {

      getCommodityDetail(id)
        .then(({ data }) =>
          this.setData({
            currentCommodity: data,
            selectedShippingMethodId: this.getFirstId(data.shippingMethod),
            selectedOptionIds: data.options.map(item =>
              this.getFirstId(item.values)
            ),
          })
        )
        .catch(err =>
          log.error(
            'CommodityDrawer.fetchCommodityDetailById.getCommodityDetail',
            err
          )
        );


    },
    getFirstId(list = []) {
      if (list.length > 0) {
        return list[0].id;
      }
      return null;
    },
    onShippingMethodSelect(id) {
      this.setData({ selectedShippingMethodId: id });
    },
    onOptionSelect(id, index) {
      const nextSelectedOptionIds = [...this.data.selectedOptionIds];
      nextSelectedOptionIds[index] = id;
      this.setData({ selectedOptionIds: nextSelectedOptionIds });
    },
    onPurchaseNumberChange(num) {
      this.setData({ purchaseNumber: num });
    },
    onConfirm() {
      // TODO: add request
      //this.props.onConfirm();
      var that = this;
      my.setStorageSync({
        key: 'params',
        data: {
          currentCommodity: this.data.currentCommodity,
        }
      });
      my.navigateTo({
        url: './saveOrder/saveOrder', // 新页面的路径，可以传递参数
      })


    },
    onTabBarTap2(e) {
      const { index } = e.target.dataset
      let sList = []
      let src = ''
      if(index == 0){
        sList = orderList
        src="../../../image/sp/spjs.png";
        //this.setData{};
        this.setData({
          src:src
        });
      }else if(index == 1){
        sList[0] = orderList[0]
        src="../../../image/sp/zllc.png";
        this.setData({
          src:src
        });
      }else if(index == 2){
        sList[0] = orderList[1]
        src="../../../image/sp/zlbd.png";
        this.setData({
          src:src
        });
      }
      this.setData({
        activeTab: index,
      });
    },
  },
});
